package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.animation.BaseAnimatorSet;
import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.activity.GonggaoDetailActivity.GongaoInfoAdapter;
import com.ycsj.goldmedalnewconcept.bean.AttResponse;
import com.ycsj.goldmedalnewconcept.bean.GonggaoInfo;
import com.ycsj.goldmedalnewconcept.bean.GonggaoResponse;
import com.ycsj.goldmedalnewconcept.bean.HomeWorkDetailResponse;
import com.ycsj.goldmedalnewconcept.bean.PayDetailResponse;
import com.ycsj.goldmedalnewconcept.bean.PayDetailResponse.PayDetailInfo;
import com.ycsj.goldmedalnewconcept.bean.StudentInfo;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.view.PullToRefreshLayout;
import com.ycsj.goldmedalnewconcept.R;

// 缴费详情列表
@SuppressLint("ResourceAsColor")
public class PayDetailActivity extends Activity implements OnRefreshListener,OnClickListener {

	private ImageView iv_back;
	private String id;
	private ListView lv_gonggao;
	private TextView tv_empty;
	private PayDetailAdapter mAdapter;
	private List<PayDetailInfo> paylist;
	private PullToRefreshLayout refreshLayout;
	private Intent intent;
	
	private LinearLayout ll_title;
	private String classnum;
	private String user_id;
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_paydetail);
//		role = SPUtil.getString(this, "role", "");
//		account = SPUtil.getString(this, "account", "");
		
		intent = getIntent();
		classnum = intent.getStringExtra("classnum");
		user_id = intent.getStringExtra("user_id");
		
		Log.e("classnum", classnum+"");
		Log.e("user_id", user_id+"");
		paylist = new ArrayList<PayDetailInfo>();

		initView();
		setOnListener();
		initData();
	}

	private void initView() {
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_empty = (TextView) findViewById(R.id.tv_empty);
		
		lv_gonggao = (ListView) findViewById(R.id.lv_gonggao);
		ll_title = (LinearLayout) findViewById(R.id.ll_title);
		refreshLayout = (PullToRefreshLayout)findViewById(R.id.refresh_view);
		refreshLayout.setOnRefreshListener(this);
	}

	private void initData() {

		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "30");
		builder.add("c", user_id);
		builder.add("classnum", classnum);
		Request request = new Request.Builder()
				.url(Constant.ycsj_count_MessageCenter).post(builder.build())
				.build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			// http://pm.moregolden.com/ycsj_count/MessageCenter?type=30&c=13006314760&classnum=1
			@Override
			public void onFailure(Request arg0, IOException arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
								.show();
					}
				});
			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();
				String json = arg0.body().string();
				Log.e("test", json);
				PayDetailResponse fromJson = gson.fromJson(json,
						PayDetailResponse.class);

				if (fromJson != null) {
					if ("-1".equals(fromJson.state)
							|| "0".equals(fromJson.state)) {
						
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								tv_empty.setVisibility(tv_empty.VISIBLE);
							}
						});
						
						
					} else if ("1".equals(fromJson.state)) {
						paylist = fromJson.list;

						mAdapter = new PayDetailAdapter();
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if(paylist.size()==0){
									tv_empty.setVisibility(tv_empty.VISIBLE);
								}
								refreshLayout
								.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
								lv_gonggao.setAdapter(mAdapter);
							}
						});
					}
				}
			}
		});
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		}
	}

	class PayDetailAdapter extends BaseAdapter {

		public int getCount() {

			return paylist.size();
		}

		public Object getItem(int position) {

			return null;
		}

		public long getItemId(int position) {

			return 0;
		}

		ViewHolder vh = null;

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(),
						R.layout.item_paydetail, null);
				vh = new ViewHolder();

				vh.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
				vh.tv_name = (TextView) convertView
						.findViewById(R.id.tv_name);
				vh.tv_pay = (TextView) convertView.findViewById(R.id.tv_pay);
				vh.tv_other = (TextView) convertView
						.findViewById(R.id.tv_other);
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}

			vh.tv_name.setText(paylist.get(position).name);
			vh.tv_pay.setText(paylist.get(position).payment);
			
			 
			Picasso.with(PayDetailActivity.this)
			 .load(paylist.get(position).icon)
			 .error(R.drawable.header).into(vh.iv_icon);
			
			
			
			return convertView;
		}

		class ViewHolder {
			private ImageView iv_icon;
			private TextView tv_name;
			private TextView tv_pay;
			private TextView tv_other;

		}
	}

	@Override
	public void onRefresh() {
		// 下拉刷新操作
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				initData();
			}
		}, 500);
	}
}







